using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.ServiceLocation;
using System.Web.Mvc;

using MvcCms.Core;
using MvcCms.Data;
using MvcCms.Data.Entities;
using MvcCms.Service.Cache;
using MvcCms.Service.Code;
using MvcCms.Service.Validation;
using MvcCms.Core.Interfaces;
using MvcCms.Service.Logging;
using MvcCms.Data.Service.Code;

namespace MvcCms.Service.CMSCode
{
    public class ZoneService : IZoneService
    {
        #region Constructors

        private readonly ICacheService _cacheService;
        private readonly Portal _currentPortal;
        private readonly ICmsRepository _repository;
        private readonly ICmsService _service;
        private readonly IThemeDataService _themeService;

        public ZoneService(ICmsRepository repository, ICacheService cacheService, ICmsService service, IThemeDataService themeService)
        {
            _repository = repository;
            _service = service;
            if (cacheService != null)
            {
                _currentPortal = cacheService.GetCurrentPortal();
            }
            _cacheService = cacheService;
            _themeService = themeService;
        }

        #endregion

        public string GetZoneHTML(Widget widget, bool isAuthenticated, string[] roles)
        {
            // we will declare zones in the database soon
            //check for weighted content to apply to the zone
            var htmlToReturn = "";
            var widgetToLoad = _service.GetNextWeightedWidgetForZone(widget.ZoneName, isAuthenticated, roles);
            if (widgetToLoad != null)
            {
                var divToReplace = "<div class=\"" + widgetToLoad.WeightedContent.ContainerClasses + "\" id=\"" + widgetToLoad.WeightedContent.TargetDiv + "\"></div>";
                htmlToReturn = widget.HTML.Replace(divToReplace, "<div class=\"" + widgetToLoad.WeightedContent.ContainerClasses + "\" id=\"" + widgetToLoad.WeightedContent.TargetDiv + "\">" + widgetToLoad.Widget.HTML + "</div>");
            }
            else
            {
                //if this is a stand alone widget not meant for weighted then just return its html
                if (!widget.IsForWeightedContent)
                {
                    //lets find out if this widget is supposed to be on this page or not
                    var currentPage = _service.GetContentPageForRequest();
                    if (currentPage != null)
                    {
                        var widgetPageId = widget.ContentPageID.GetValueOrDefault();
                        var widgetCatId = widget.ContentCategoryID.GetValueOrDefault();

                        if (widgetPageId == currentPage.ContentPageID) { htmlToReturn = widget.HTML; }
                        if (widgetCatId == currentPage.ContentCategory.CategoryID) { htmlToReturn = widget.HTML; }
                    }
                }
            }
            return htmlToReturn;
        }

        public IEnumerable<Zone> ListZones(int? themeId)
        {
            var themeIdToUse = 0;
            if (themeId.HasValue)
            {
                themeIdToUse = themeId.GetValueOrDefault();
            }
            if (_currentPortal.DefaultThemeID.HasValue)
            {
                themeIdToUse = _currentPortal.DefaultThemeID.GetValueOrDefault();
            }

            if (themeIdToUse != 0)
            {
                return _themeService.ListZones(_currentPortal.DefaultThemeID.GetValueOrDefault());
            }
            else
            {
                return null;
            }
        }

    }
}